Skip to content

Conversation

@karlseguin
Copy link
Collaborator

Prototype resolution of Zig types previously had 2 limitations (bug?). The first was that the Zig prototype chain could only be 1 deep. You couldn't do A->B->C where each of those was a Zig type (but you could do A->B->C->D->E ... so long as every other type was a C opaque value).

The other limitation was that Zig prototypes only worked when the nested field was directly embedded in the struct (i.e. not a pointer). So you could do:

const X = struct {
   proto: XParent,
};

But not:

const X = struct {
   proto: *XParent,
};

This addresses both limitations. The first issue is solved by keeping track of the cumulative offset

Prototype resolution of Zig types previously had 2 limitations (bug?). The first
was that the Zig prototype chain could only be 1 deep. You couldn't do A->B->C
where each of those was a Zig type (but you could do A->B->C->D->E ... so long
as every other type was a C opaque value).

The other limitation was that Zig prototypes only worked when the nested field
was directly embedded in the struct (i.e. not a pointer). So you could do:

```zig
const X = struct {
   proto: XParent,
};
```

But not:

```zig
const X = struct {
   proto: *XParent,
};
```

This addresses both limitations. The first issue is solved by keeping track
of the cumulative offset
while (true) {
const proto_offset = meta_lookup[type_index].proto_offset;
if (proto_offset < 0) {
base_ptr = @as(*align(1) usize, @ptrFromInt(base_ptr + total_offset + @as(usize, @intCast(-proto_offset)))).*;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is black magic 🪄

@karlseguin karlseguin merged commit b8f3b19 into main Jul 9, 2025
10 checks passed
@karlseguin karlseguin deleted the improved_native_proto branch July 9, 2025 02:01
@github-actions github-actions bot locked and limited conversation to collaborators Jul 9, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants